package org.example.controller;

import com.github.pagehelper.PageHelper;
import org.example.model.Student;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询学生信息（数据字段：获取下拉菜单选项）=> 通过班级id查询关联的学生
    //GET /api/student/queryAsDict?dictionaryKey=2
    @RequestMapping("/queryAsDict")
    public Object queryAsDict(@RequestParam("dictionaryKey") Integer classesId){
        List<Student> students = studentService.queryAsDict(classesId);
        return students;
    }

    //查询学生信息（表格）
    //GET /api/student/query
    @RequestMapping("/query")
    public Object query(Student student){
        //分页的使用：先设置一下分页
        PageHelper.startPage(student);
        List<Student> students = studentService.query();
        //使用完，清理设置
        PageHelper.clearPage();
        return students;
    }

    //新增学生
    //POST /api/student/add
    //{"studentName":"社发顺丰","studentNo":"收到","idCard":"","classesId":"1","studentEmail":""}
    @RequestMapping("/add")
    public Object add(@RequestBody Student student){
        int n = studentService.add(student);
        return null;
    }

    //获取学生详细信息（修改时，会先查询）
    //GET /api/student/queryById?id=xxx
    @RequestMapping("/queryById")
    public Object queryById(Integer id){
        Student student = studentService.queryById(id);
        return student;
    }

    //修改学生信息（和新增学生格式类似，多个id字段）
    //POST /api/student/update
    //{"id": 1, "studentName":"社发顺丰","studentNo":"收到","idCard":"","classesId":"1","studentEmail":""}
    @RequestMapping("/update")
    public Object update(@RequestBody Student student){
        int n = studentService.update(student);
        return null;
    }

    //删除学生（一个或多个）
    //GET /api/student/delete?ids=xxx&ids=xxx
    @RequestMapping("/delete")
    public Object delete(@RequestParam List<Integer> ids){
        int n = studentService.delete(ids);
        return null;
    }
}
